package com.example.administrator.project;

import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;
public class MainActivity extends AppCompatActivity {

    Button read,add;
    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        SQLiteStudioService.instance().start(this);
        dbHelper=new MyDatabaseHelper(this,"BookStore.db",null,1);
        read=(Button)findViewById(R.id.read);
        add=(Button)findViewById(R.id.add);
        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


               new Thread(new Runnable() {
                   @Override
                   public void run() {



                       //第一步：实例化URL对象
                       String address="https://www.tianqiapi.com/api/?version=v6"; //设置接口
                       try {
                           URL url =new URL(address);   //实例化URL对象
                           //实例化 HttpURLConnection对象
                           HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                           //设置链接属性
                           conn.setRequestMethod("GET");//设置请求方法
                           conn.setReadTimeout(5000);//设置超时时间
                           if(conn.getResponseCode()==200){ //获取状态码 200表示连接成功
                               //获取输入流
                               InputStream in= conn.getInputStream();
                               //读取输入流
                               byte[] b=new byte[1024*512]; //定义一个byte数组读取输入流
                               ByteArrayOutputStream baos = new ByteArrayOutputStream(); //定义缓存流来保存输入流的数据
                               int len=0;
                               while((len=in.read(b))>-1){  //每次读的len>-1 说明是是有数据的
                                   baos.write(b,0,len);  //三个参数  输入流byte数组   读取起始位置  读取终止位置
                               }
                               String msg=baos.toString();
                               Log.e("TAG112345",msg);
                           }
                       } catch (java.io.IOException e) {
                           e.printStackTrace();
                       }

                   }
               }).start();






            }
        });

    }

}